Ext.define("App.datacollection.collectionschedule.view.CollectionScheduleEditor", {
    extend: 'Extend.window.FormWindow',
    alias: 'widget.CollectionScheduleEditor',
    entity: 'entity',
    requires: ['App.commons.field.MonthDaysField',
        'App.commons.field.WeekDaysField', 'Extend.form.field.StringDatefield'],
    config: {
        window: {
            title: '采集计划编辑',
            width: 900,
            height: 500,
            resizable: false
        }
    },
    defaults: {
        columnWidth: 1
    },
    items: [{
        xtype: 'hidden',
        name: 'id'
    }, {
        xtype: 'hidden',
        name: 'projectId'
    }, {
        xtype: 'hidden',
        name: 'startDatetime'
    }, {
        xtype: 'textfield',
        fieldLabel: '计划名称',
        name: 'name',
        allowBlank: false,
        blankText: '接口名称为必填字段，不能为空！',
        maxLength: 256,
        columnWidth: .5,
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        xtype: 'booleancombobox',
        name: 'disabled',
        fieldLabel: '停用',
        allowBlank: false,
        blankText: '停用为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        fieldLabel: '计划任务',
        xtype: 'combobox',
        name: 'task.id',
        editable: false,
        multiSelect: false,
        displayField: 'name',
        valueField: 'id',
        allowBlank: false,
        blankText: '接口为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        columnWidth: .5,
        store: {
            fields: ['id', 'name'],
            pageSize: 999,
            proxy: {
                type: 'ajax',
                actionMethods: {
                    read: 'POST'
                },
                url: 'datacollection/collectiontask/query_simple',
                reader: {
                    type: 'json',
                    rootProperty: 'data'
                }
            },
            autoLoad: true
        },
        listeners: {
            select: function ($this, ai) {
                var view = this.up();
                if (!Ext.isEmpty(ai)) {
                    view.resetParameters(ai.get('parameters'));
                }
            }
        }
    }, {
        xtype: 'datefield',
        name: 'startDate',
        format: 'Y-m-d',
        editable: false,
        fieldLabel: '计划执行时间',
        columnWidth: .3,
        listeners: {
            select: function () {
                var date = this;
                var time = this.up().down("textfield[name='startTime']");
                var datetime = this.up().down("hidden[name='startDatetime']");
                var xx = date.getRawValue() + " " + time.getRawValue();
                datetime.setRawValue(xx);
            }
        }
    }, {
        xtype: 'timefield',
        name: 'startTime',
        format: 'H:i:s',
        editable: true,
        columnWidth: .2,
        listeners: {
            select: function () {
                var time = this;
                var date = this.up().down("textfield[name='startDate']");
                var datetime = this.up().down("hidden[name='startDatetime']");
                var xx = date.getRawValue() + " " + time.getRawValue();
                datetime.setValue(xx);
            }
        }
    }, {
        xtype: 'textfield',
        fieldLabel: '接口描述',
        name: 'description'
    }, {
        xtype: 'textfield',
        fieldLabel: 'CRON',
        name: 'cron'
    }]
});


Ext.define("App.datacollection.collectiontask.view.ScheduleSetting", {
    extend: 'Ext.panel.Panel',
    alias: ['widget.ScheduleSetting'],
    layout: 'column',
    requires: ['App.commons.field.DateTime'],
    defaults: {
        labelAlign: 'right',
        labelWidth: 80,
        margin: 5,
        columnWidth: .51
    },
    items: [{
        fieldLabel: '重复模式',
        xtype: 'combobox',
        name: 'repeatMode',
        editable: false,
        allowBlank: false,
        blankText: '重复模式为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        store: ['周期性重复', '每月', '每周', '高级'],
        listeners: {
            change: function (combo, nv, ov) {
                var cardBox = this.up('CollectionTaskEditor').down('[itemId="card"]');
                cardBox.getLayout().getActiveItem().disable();
                var target = this.up('CollectionTaskEditor').down('')
                if (nv == '周期性重复') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="periodic"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                } else if (nv == '高级') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="custom"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                } else if (nv == '每周') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="week"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                } else if (nv == '每月') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="month"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                }
            }
        }
    }, {
        fieldLabel: '是否挂起',
        xtype: 'BooleanComboBox',
        name: 'disabled',
        allowBlank: false,
        blankText: '是否禁用为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        fieldLabel: '开始时间',
        xtype: 'datetimefield',
        name: 'triggerStartDatetime',
        emptyText: '为空时立即开始',
        //editable: false,
        format: 'Y-m-d h:i'
    }, {
        xtype: 'fieldset',
        title: '计划配置',
        layout: 'card',
        itemId: 'card',
        columnWidth: 1,
        padding: '5 0 5 0',
        defaults: {
            layout: 'column',
            xtype: 'fieldcontainer',
            disabled: true,
            defaults: {
                labelAlign: 'right',
                labelWidth: 100,
                margin: 5
            }
        },
        items: [{
            itemId: 'week',
            items: [{
                fieldLabel: '执行时间',
                xtype: 'timefield',
                name: 'executeTime',
                columnWidth: .5,
                format: 'H:i',
                blankText: '执行时间为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }, {
                xtype: 'weekdaysfield',
                fieldLabel: '周几执行',
                columnWidth: 1,
                name: 'executeDays',
                allowBlank: false,
                blankText: '周几执行为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }]
        }, {
            itemId: 'month',
            items: [{
                fieldLabel: '执行时间',
                xtype: 'timefield',
                name: 'executeTime',
                columnWidth: .51,
                format: 'H:i',
                allowBlank: false,
                blankText: '执行时间为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }, {
                xtype: 'monthdaysfield',
                fieldLabel: '执行日期',
                columnWidth: 1,
                name: 'executeDays',
                allowBlank: false,
                blankText: '执行日期为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }]
        }, {
            itemId: 'periodic',
            items: [{
                fieldLabel: '执行间隔（秒）',
                xtype: 'numberfield',
                decimalPrecision: 0,
                name: 'intervalSeconds',
                columnWidth: .51,
                allowBlank: false,
                blankText: '执行间隔为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }, {
                fieldLabel: '执行次数',
                xtype: 'numberfield',
                decimalPrecision: 0,
                name: 'repeatTotalCount',
                columnWidth: .51,
                allowBlank: false,
                blankText: '执行次数为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }]
        }, {
            itemId: 'custom',
            items: [{
                fieldLabel: '计划表达式',
                xtype: 'textfield',
                name: 'cronExpression',
                columnWidth: .51,
                allowBlank: false,
                blankText: '计划表达式为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
                emptyText: '*/5 * * * * ?'
            }, {
                xtype: 'container',
                columnWidth: 1,
                html: '<div><p>Provides a parser and evaluator for unix-like cron expressions.</p><table style="font-size: 11px"><tbody><tr><td><b>Example</b></td><td><b>Description</b></td></tr><tr><td><code>0 0 12 * * ?</code></td><td>Fire at 12pm (noon) every day</td></tr><tr><td><code>0 15 10 ? * *</code></td><td>Fire at 10:15am every day</td></tr><tr><td><code>0 15 10 * * ?</code></td><td>Fire at 10:15am every day</td></tr><tr><td><code>0 15 10 * * ? *</code></td><td>Fire at 10:15am every day</td></tr><tr><td><code>0 15 10 * * ? 2005</code></td><td>Fire at 10:15am every day during the year 2005</td></tr><tr><td><code>0 * 14 * * ?</code></td><td>Fire every minute starting at 2pm and ending at 2:59pm, every day</td></tr><tr><td><code>0 0/5 14 * * ?</code></td><td>Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day</td></tr><tr valign="top"><td><code>0 0/5 14,18 * * ?</code></td><td>Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day</td></tr><tr><td><code>0 0-5 14 * * ?</code></td><td>Fire every minute starting at 2pm and ending at 2:05pm, every day</td></tr><tr><td><code>0 10,44 14 ? 3 WED</code></td><td>Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.</td></tr><tr><td><code>0 15 10 ? * MON-FRI</code></td><td>Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday</td></tr><tr><td><code>0 15 10 15 * ?</code></td><td>Fire at 10:15am on the 15th day of every month</td></tr><tr><td><code>0 15 10 L * ?</code></td><td>Fire at 10:15am on the last day of every month</td></tr><tr><td><code>0 15 10 ? * 6L</code></td><td>Fire at 10:15am on the last Friday of every month</td></tr><tr valign="top"><td nowrap=""><code>0 15 10 ? * 6L 2002-2005 &nbsp; &nbsp;</code></td><td>Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005</td></tr><tr><td><code>0 15 10 ? * 6#3</code></td><td>Fire at 10:15am on the third Friday of every month</td></tr></tbody></table></div>'
            }]
        }]
    }]
})